﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Configuration;
using KOP.Services.ServiceLibrary;
using KOP.Services.App_Data;

namespace KOP.Services
{
    /// <summary>
    /// Summary description for KOP
    /// </summary>
    [WebService(Namespace = "http://kop.kalitte.com.tr/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    // [System.Web.Script.Services.ScriptService]
    public class KOP : System.Web.Services.WebService
    {

        [WebMethod]
        public VersionResult CheckVersion(int major, int minor)
        {            
            int servicemajor = System.Convert.ToInt32(ConfigurationManager.AppSettings["AddInUpdateMajor"]);
            int serviceminor = System.Convert.ToInt32(ConfigurationManager.AppSettings["AddInUpdateMinor"]);
            if (major >= servicemajor && minor >= serviceminor)
            {
                return null;
            }
            else
            {
                VersionResult vr = new VersionResult(servicemajor, serviceminor, ConfigurationManager.AppSettings["AddInUpdateUrl"]);
                return vr;
            }
        }

        [WebMethod]
        public TemplateUpdateResult GetTemplateUpdates(TemplateType type, DateTime lastupdate)
        {
            DataClassesDataContext dc = new DataClassesDataContext();
            var info =
                from item in dc.Templates
                where item.DateTime > lastupdate && item.Type == type.ToString()
                select item;
            if (info.Count() < 1) return null;
            TemplateUpdateResult result = new TemplateUpdateResult();
            foreach (var item in info)
            {
                result.Add(new TemplateInfo(item.FilePath, item.Blob.ToArray()));
            }
            return result;
        }
    }
}
